package com.rongshu.widget;


import android.content.Context;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.rongshu.R;


public class BanyanRatingBar extends LinearLayout implements android.view.View.OnTouchListener {


    private ImageView image1;
    private ImageView image2;
    private ImageView image3;
    private ImageView image4;
    private ImageView image5;
    private int mProgress = 0;
    private Drawable defDraw;
    private Drawable highDraw;
    private Drawable lowDraw;
    private OnProgressChangeListener listener = null;
    private View ratingView;
    private boolean touch;

    public interface OnProgressChangeListener {
        void onProgressChange(View view, int progress);
    }

    public void setOnProgressChangeListener(View view, OnProgressChangeListener listener, boolean touch) {
        ratingView = view;
        this.listener = listener;
        this.touch = touch;
    }

    public BanyanRatingBar(Context context, AttributeSet attrs) {
        super(context, attrs);


        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        inflater.inflate(R.layout.layout_rating_bar, this);
        image1 = (ImageView) findViewById(R.id.rating_one);
        image2 = (ImageView) findViewById(R.id.rating_two);
        image3 = (ImageView) findViewById(R.id.rating_three);
        image4 = (ImageView) findViewById(R.id.rating_four);
        image5 = (ImageView) findViewById(R.id.rating_five);
        initDrawable();
        image1.setOnTouchListener(this);
        image2.setOnTouchListener(this);
        image3.setOnTouchListener(this);
        image4.setOnTouchListener(this);
        image5.setOnTouchListener(this);
    }

    public void setSize(int x, int y) {
        image1.setLayoutParams(new LayoutParams(x, y));
        image2.setLayoutParams(new LayoutParams(x, y));
        image3.setLayoutParams(new LayoutParams(x, y));
        image4.setLayoutParams(new LayoutParams(x, y));
        image5.setLayoutParams(new LayoutParams(x, y));
    }

    public void destroy() {
        if (defDraw != null) {
            defDraw = null;
        }

        if (highDraw != null) {
            highDraw = null;
        }

        if (lowDraw != null) {
            lowDraw = null;
        }
    }

    private void initDrawable() {
        if (defDraw == null) {
            defDraw = getResources().getDrawable(R.drawable.banyan_rating_default);
        }

        if (highDraw == null) {
            highDraw = getResources().getDrawable(R.drawable.banyan_rating_selected);
        }

        if (lowDraw == null) {
            lowDraw = getResources().getDrawable(R.drawable.banyan_rating_selected);
        }
    }

    public void notifyRatingBar() {
        initDrawable();
        setProgress(mProgress);
    }

    public int getProgress() {
        return mProgress;
    }

    @SuppressWarnings("deprecation")
    public void setProgress(int progress) {
        mProgress = progress;
        boolean isMatch = false;
        switch (progress) {

            case 1:
                image1.setBackgroundDrawable(lowDraw);
                image2.setBackgroundDrawable(defDraw);
                image3.setBackgroundDrawable(defDraw);
                image4.setBackgroundDrawable(defDraw);
                image5.setBackgroundDrawable(defDraw);
                isMatch = true;
                break;

            case 2:
                image1.setBackgroundDrawable(lowDraw);
                image2.setBackgroundDrawable(lowDraw);
                image3.setBackgroundDrawable(defDraw);
                image4.setBackgroundDrawable(defDraw);
                image5.setBackgroundDrawable(defDraw);
                isMatch = true;
                break;

            case 3:
                image1.setBackgroundDrawable(highDraw);
                image2.setBackgroundDrawable(highDraw);
                image3.setBackgroundDrawable(highDraw);
                image4.setBackgroundDrawable(defDraw);
                image5.setBackgroundDrawable(defDraw);
                isMatch = true;
                break;

            case 4:
                image1.setBackgroundDrawable(highDraw);
                image2.setBackgroundDrawable(highDraw);
                image3.setBackgroundDrawable(highDraw);
                image4.setBackgroundDrawable(highDraw);
                image5.setBackgroundDrawable(defDraw);
                isMatch = true;
                break;

            case 5:
                image1.setBackgroundDrawable(highDraw);
                image2.setBackgroundDrawable(highDraw);
                image3.setBackgroundDrawable(highDraw);
                image4.setBackgroundDrawable(highDraw);
                image5.setBackgroundDrawable(highDraw);
                isMatch = true;
                break;

            default:
                break;
        }

        if (isMatch && listener != null && ratingView != null) {
            listener.onProgressChange(ratingView, progress);
        }
    }

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        if (!touch) {
            return false;
        }

        boolean isMatch = false;
        int id = v.getId();
        if (id == R.id.rating_one) {
            mProgress = 1;
            isMatch = true;
        } else if (id == R.id.rating_two) {
            mProgress = 2;
            isMatch = true;
        } else if (id == R.id.rating_three) {
            mProgress = 3;
            isMatch = true;
        } else if (id == R.id.rating_four) {
            mProgress = 4;
            isMatch = true;
        } else if (id == R.id.rating_five) {
            mProgress = 5;
            isMatch = true;
        }
        if (isMatch) {
            setProgress(mProgress);

        }

        return false;
    }

}
